Component({
  properties: {
    cover: {
      type: String,
      default: "",
    },
    skuList: {
      type: Array,
      value: () => [],
    },
    specList: {
      type: Array,
      value: () => [],
    },
    categoryList: {
      type: Array,
      value: () => [],
    },
    selectSkuId: {
      type: Number,
      default: 0,
    },
    selectCategoryId: {
      type: Number,
      default: 0,
    },
    selectPlanId: {
      type: Number,
      default: 0,
    },
    isShowCategoryList: {
      type: Boolean,
      default: true,
    },
  },
  data: {
    specSelect: [0, 0, 0, 0, 0, 0],
    specTitle: [],
    skuId: 0,
    categoryId: 0,
    planId: 0,
    imgUrl: "",
    price: 0,
    stockNum: 0,
  },
  ready() {
    console.log("执行ready");
    const skuList = JSON.parse(JSON.stringify(this.data.skuList));
    if (
      this.data.selectCategoryId !== null &&
      this.data.selectCategoryId !== undefined
    ) {
      this.setData({
        categoryId: this.data.selectCategoryId,
        planId: this.data.selectPlanId,
      });
    }
    const categoryInfo = this.data.categoryList.filter(
      (v) => v.id === this.data.categoryId
    )[0];
    if (this.data.selectSkuId > 0) {
      const skuInfo = skuList.filter(
        (v) => v.skuId === this.data.selectSkuId
      )[0];
      const totalPay = skuInfo?.price + (categoryInfo?.price || 0);
      console.log("ready skuInfo", skuInfo);
      this.setData({
        specSelect: skuInfo?.specAttrs,
        imgUrl: skuInfo?.imgUrl,
        price: (totalPay / 100).toFixed(2),
        skuPrice: skuInfo?.price / 100,
        stockNum: skuInfo?.stockNum,
      });
      let specTitle = this.data.specTitle;
      skuInfo?.specAttrs.forEach((v) => {
        this.data.specList.forEach((spi, spIndex) => {
          spi.attrList.map((attri) => {
            if (v === attri.id) {
              console.log(v, attri);
              specTitle[spIndex] = attri.title;
              this.setData({
                specTitle,
              });
            }
          });
        });
        //
      });
      console.log("this.data.specList", this.data.specList);
      this.handleSetDisableSku();
    }
  },
  methods: {
    //商品选择器
    handleSelectCategory(e) {
      const {
        currentTarget: {
          dataset: { selectitem },
        },
      } = e;
      console.log("选择了资源", selectitem, this.data.skuPrice);
      this.setData({
        categoryId: selectitem.id,
        planId: selectitem.planId,
        price: (selectitem.price / 100 + this.data.skuPrice).toFixed(2),
      });
      this.triggerEvent("selectCategory", selectitem);
    },
    handleShowSelectGood() {
      this.triggerEvent("close");
    },
    handleSubmitSelectGoods() {
      if (this.data.skuId !== null && this.data.skuId !== undefined) {
        this.triggerEvent("select", {
          skuId: this.data.skuId,
          specTitle: this.data.specTitle.join(","),
        });
      }
    },
    handleSetDisableSku(auto = false, selectitem) {
      const skuList = JSON.parse(JSON.stringify(this.data.skuList));
      const specList = JSON.parse(JSON.stringify(this.data.specList));
      let {
        specSelect: spec,
        specTitle,
        categoryList,
        categoryId,
      } = JSON.parse(JSON.stringify(this.data));
      const categoryInfo = categoryList.filter((v) => v.id === categoryId)[0];
      //1，拿到所有可选的sku.specAttrs
      const canChooseSku = skuList.filter((sku) =>
        sku.specAttrs.includes(spec[0])
      );
      console.log("canChooseSku", canChooseSku);
      //2，筛选出specList中包含sku.specAttrs的选择
      let specAttrs = [];
      canChooseSku.forEach((ccSku) => {
        specAttrs.push(...ccSku.specAttrs);
      });
      specAttrs = [...new Set(specAttrs)];
      //3，进行不可选
      specList.map((spc, spcIndex) => {
        spc.attrList.map((spAttr) => {
          if (spcIndex !== 0) {
            spAttr.disable = true;
            if (specAttrs.includes(spAttr.id)) {
              spAttr.disable = false;
            }
          } else {
            spAttr.disable = false;
          }
        });
      });
      this.setData({
        specList,
      });
      //4,当第不可选择的时候，
      if (auto) {
        const canChooseSku = skuList.filter((sku) =>
          sku.specAttrs.includes(selectitem.id)
        );
        const item = canChooseSku[0];
        if (!item) return;
        spec = canChooseSku[0].specAttrs;
        canChooseSku[0].specAttrs.map((sku, selectindex) => {
          if (sku > 0) {
            specTitle[selectindex] = specList[selectindex].attrList.filter(
              (attr) => attr.id === sku
            )[0]?.title;
          }
        });
        console.log("当第不可选择的时候", specList, item, spec);
        this.setData({
          specSelect: spec,
          specTitle,
          skuId: item.skuId,
          imgUrl: item.imgUrl,
          price: (((categoryInfo?.price || 0) + item.price) / 100).toFixed(2),
          skuPrice: item.price / 100,
          stockNum: item.stockNum,
        });
        this.handleSetDisableSku();
      }
    },
    handleSelectGoods(e) {
      const skuList = JSON.parse(JSON.stringify(this.data.skuList));
      const specList = JSON.parse(JSON.stringify(this.data.specList));
      const categoryInfo = this.data.categoryList.filter(
        (v) => v.id === this.data.categoryId
      )[0];
      const {
        currentTarget: {
          dataset: { selectindex, selectitem },
        },
      } = e;
      const { specSelect: spec, specTitle } = JSON.parse(
        JSON.stringify(this.data)
      );
      // let spec = this.data.specSelect;
      // let specTitle = this.data.specTitle;
      spec[selectindex] = selectitem.id;
      specTitle[selectindex] = selectitem.title;
      let isCanChooseGoods = false;
      // console.log("选择商品,", selectitem, spec);
      skuList.forEach((v) => {
        if (
          v.specAttrs[0] === spec[0] &&
          v.specAttrs[1] === spec[1] &&
          v.specAttrs[2] === spec[2] &&
          v.specAttrs[3] === spec[3] &&
          v.specAttrs[4] === spec[4] &&
          v.specAttrs[5] === spec[5]
        ) {
          isCanChooseGoods = true;
          console.log("选择了商品", categoryInfo?.price, v.price);
          this.setData({
            specSelect: spec,
            specTitle,
            skuId: v.skuId,
            imgUrl: v.imgUrl,
            price: (((categoryInfo?.price || 0) + v.price) / 100).toFixed(2),
            skuPrice: v.price / 100,
            stockNum: v.stockNum,
          });
          this.handleSetDisableSku();
        }
      });
      if (!isCanChooseGoods) {
        if (selectindex === 0) {
          this.handleSetDisableSku(true, selectitem);
        }
        // wx.showToast({ title: `${specTitle}  已下架`, icon: "none" });
      }
    },
  },
});
